﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Book_of_Shadow_Script.Scripts
{
    class oreimou2bDetail:ScriptDetail
    {
        int idnum;
        public override int len()
        {
            int ret=BitConverter.ToInt32(buf, 0);
            return ret;
        }
        public oreimou2bDetail(long offset, int order, CustomEncoding en = null)
            : base(offset, order-1, en)
        { }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            String type = "";
            sb.AppendFormat("{0:X6}: {1:X4} ", base.Offset, base.Order);
            switch (ScpType())
            {
                case 0x64:
                case 0x68: type = this.text; break;//文本
                case 0x69: type = this.text; break;//文本
                case 0x515: type = "ORE出现"; break;//ORE
                case 0x2C2: type = this.text; break;//动画
                case 0x2BC: type = this.text; break;//场景标题
                case 0x2BE: type = this.text; break;//跳转
            }
            if (type != "")
            {
                sb.Append("  ");
                sb.Append(type);
            }
            return sb.ToString();
        }
        public override string DisplayByte()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("{0:X4} ", BitConverter.ToInt32(buf,0));
            sb.Append(Environment.NewLine);
            sb.AppendFormat("{0:X4} ", BitConverter.ToInt16(buf, 4));
            sb.Append(Environment.NewLine);
            if (hasText())
            {
                sb.AppendFormat("{0:X2} ", buf[6]);
                sb.Append(Environment.NewLine);
                for (int i = 7; i < len(); i += 2)
                    sb.AppendFormat("{1:X2}{0:X2} ", buf[i], buf[i + 1]);
            }
            else
            {
                for (int i = 6; i < len(); i += 2)
                    sb.AppendFormat("{1:X2}{0:X2} ", buf[i], buf[i + 1]);
            }
            return sb.ToString();
        }

        public override int ScpType()
        {
            int ret = BitConverter.ToInt16(buf, 4);
            return ret;
        }

        public override void analyze(System.IO.FileStream fs)
        {
            if (ScpType() == 0x64 || ScpType() == 0x68)//文本
            {
                idnum=BitConverter.ToInt16(buf,9);
                int strlen=BitConverter.ToInt32(buf,11);
                this.text = string.Format("{0:X2}",(Int16)idnum);
                this.text += Environment.NewLine;
                this.text += new string(Encoding.Unicode.GetChars(buf, 15, strlen * 2));
            }
            if (ScpType() == 0x69)
            {
                int cnt = BitConverter.ToInt32(buf, 6);
                StringBuilder sb=new StringBuilder();
                int now=6;
                now += 4;
                for (int i = 0; i < cnt; i++)
                {
                    int id = BitConverter.ToInt32(buf, now);
                    now+=4;
                    int strlen=BitConverter.ToInt32(buf, now);
                    now += 4;
                    string str = new string(Encoding.Unicode.GetChars(buf, now, strlen * 2));
                    now += strlen * 2 + 4;
                    sb.Append(String.Format("{0:X2} ",id));
                    sb.Append(str);
                    sb.Append(Environment.NewLine);
                }
                this.text = sb.ToString();

            }
            if (ScpType() == 0x2BC)//场景标题
            {
                int strlen = BitConverter.ToInt32(buf, 6);
                this.text += new string(Encoding.Unicode.GetChars(buf, 10, strlen * 2));
            }
            if (ScpType() == 0x2C2)//动画
            {
                int strlen = BitConverter.ToInt32(buf, 6);
                this.text += new string(Encoding.Unicode.GetChars(buf, 10, strlen * 2));
            }
            if (ScpType() == 0x2BE) //跳转
            {
                int param = BitConverter.ToInt32(buf, 6);
                this.text = "跳转至";
                this.text += String.Format("{0:X4}", param);
            }
        }
        public override string TextOut()
        {
            throw new NotImplementedException();
        }

        public override bool hasText()
        {
            return this.ScpType() == 0x64 || this.ScpType() == 0x68;
        }
        public override void ReadHead(System.IO.FileStream fs)
        {
            fs.Read(buf, 0, 4);
            int count = BitConverter.ToInt32(buf, 0);
            fs.Read(buf, 4, 4);
            int h_size = BitConverter.ToInt32(buf, 4);
            fs.Read(buf, 8, h_size - 8);
        }
    }
}
